﻿namespace ThunderboltIocFrame
{
	/// <summary>
	/// 装饰类
	/// </summary>
	public class ServiceDescriptor
	{
		/// <summary>
		/// 基类型（如果有的话）
		/// </summary>
		public readonly Type ParentType;
		/// <summary>
		/// 实现类型
		/// </summary>
		public readonly Type ImplementType;
		/// <summary>
		/// 单例对象（如果注册单例的话）
		/// </summary>
		public object? Singleton { get; set; }
		/// <summary>
		/// 注册类型
		/// </summary>
		public LifeCycle LifeCycleState;

		public ServiceDescriptor(Type implementType, LifeCycle lifeCycleState):this(implementType, implementType, lifeCycleState)
		{
		
		}
		public ServiceDescriptor(Type implementType, Type parentType, LifeCycle lifeCycleState)
		{
			this.ParentType = parentType;
			this.ImplementType = implementType;
			this.LifeCycleState = lifeCycleState;
		}

		public object? GetInstance()=>
				LifeCycleState switch
				{
					LifeCycle.Singleton => Singleton ?? Activator.CreateInstance(ImplementType),
					LifeCycle.Transient => Activator.CreateInstance(ImplementType),
					LifeCycle.Scope => Activator.CreateInstance(ImplementType),
					_ => throw new NotImplementedException("枚举不存在"),
				};
	}
	/// <summary>
	/// 生命周期
	/// </summary>
	public enum LifeCycle
	{
		/// <summary>
		/// 单例类
		/// </summary>
		Singleton,
		/// <summary>
		/// 范围类
		/// </summary>
		Scope,
		/// <summary>
		/// 瞬态类
		/// </summary>
		Transient
	}
}